$('#add-ticket').click(function () {
    show($('#add-work-order'));
    document.body.style.overflow = 'hidden';
});
let start = 1;
let pageSize = 10;
$('#watch-ticket').click(function () {
    show($('#watch-work-order'));
    queryAllWorkOrder(start,pageSize)
});

$('#approve').click(function () {
    show($('#todo'));
});

$('#close-details').click(function () {
    $('#approval-details')
        .transition('swing right')
    ;
});

$(document).on('dblclick','.copy',function () {
    let text = $(this).children().first().html();
    let clipboard = new ClipboardJS('.copy', {
        text: () => {
            //目标文本 意味着可以动态设置text
            return text;
        },
    });
    clipboard.on("success", function (e) {

    });
    clipboard.on("error", function (e) {
        // do something
    });
});


$('.ui.search')
    .search({
        apiSettings: {
            //url: '//api.github.com/search/repositories?q={query}'
            url: '/plan/searchTicketInfo?val={query}'
        },
        fields: {
            results: 'data',
            title: 'ticket'
        },
        minCharacters: 3
    })
;
let hi = $('#heart-icon');
let sp = $('#searchVal');
sp.focus(function () {
    hi.addClass('h-i-c');
});
sp.blur(function () {
    hi.removeClass('h-i-c');
});

let owner;
let ff = true;
$(document).on('click','.as-content-cc',function () {
    pack_detail_info($(this).attr('ap-id'));
    //visible
    let ad = $('#approval-details');
    if (ad.hasClass('visible') && $(this).attr('ap-id') === owner.attr('ap-id')) {
        ad.transition('swing right');
    }
    if (ff || ad.hasClass('hidden')) {
        ff = false;
        ad.transition('swing right');
    }
    owner = $(this);
});

function pack_detail_info(id){
    $.get({
        url: '/flow/getDetail/'+id,
        success:function (result){
            if (result.status === 200) {
                let data=result.data;
                $('#app-id').text(data.id);
                $('#app-name').text(data.name);
                $('#app-img').attr('src',data.img)
                $('#app-department').text(data.department);
                $('#app-type').text(data.type);
                $('#app-time').text(data.createTime);
                $('#app-workOrderNumber').text(data.workOrderNumber);
                $('#app-productCode').text(data.productCode);
                $('#app-l1').text(data.plNames);
                $('#app-l2').text(data.psNames);
                $('#app-l3').text(data.craNames);
                $('#app-l4').text(data.qlNames);
                $('#app-l5').text(data.planNames);
                $('#app-s1').text(data.proS);
                $('#app-s2').text(data.qsS);
                $('#app-s3').text(data.craS);
                $('#app-s4').text(data.qlS);
                $('#app-s5').text(data.plS);
            }
        }
    })
}

function show(item) {
    scroll(0, 0);
    item.show();
    item.siblings().not('nav').hide();
    document.body.style.overflow = 'visible'
}

function canvas(data) {
    let myChart = echarts.init(document.getElementById('main2'));
    let option;
    option = {
        legend: {
            // x: 'right',
            //
            // y: 'center',
            //padding: [200, 0, 0, 140],
            // //图例的排列默认属性是：水平或垂直,默认是：水平
            orient: 'horizontal',
            bottom: 'bottom',
            "textStyle": {
                "fontSize": 14,
            }
        },
        tooltip: {
            "trigger": "item",
            "textStyle": {
                "fontSize": 14
            }
        },
        series: [
            {
                name: '',
                type: 'pie',
                radius: [50, 135],
                center: ['50%', '50%'],
                roseType: 'area',
                itemStyle: {
                    borderRadius: 8
                },
                "label": {
                    "normal": {
                        "show": true,
                        "textStyle": {
                            "fontSize": 14
                        }
                    },
                    "emphasis": {
                        "show": true
                    }
                },
                "lableLine": {
                    "normal": {
                        "show": true
                    },
                    "emphasis": {
                        "show": true
                    }
                },
                data: data
            }
        ]
    };
    myChart.setOption(option);
}


//****************************
laydate.render({
    elem: '#planBeginTime'
    //设置开始日期、日期日期的 input 选择器
    //数组格式为 5.3.0 开始新增，之前版本直接配置 true 或任意分割字符即可
    // , range: ['#planBeginTime', '#planEndTime']
});
laydate.render({
    elem: '#planEndTime'
    //设置开始日期、日期日期的 input 选择器
    //数组格式为 5.3.0 开始新增，之前版本直接配置 true 或任意分割字符即可
    // , range: ['#planBeginTime', '#planEndTime']
});
let content;

$(function (){
    init();
})
$('nav .head-item').click(function () {
    $(this).siblings().removeClass("active");
    $(this).addClass("active");
});


function init(){
    $.get({
        url: '/plan/init',
        success: function (result) {
            if (result.status === 200) {
                canvas(result.data.echartsVos);
                package_time(result.data.times);
                package_approve_info(result.data.taskVos,result.data.tasks);
            }
        }
    })
}
function package_time(data) {
    package_menu(data.year, $('#default-year-menu'));
    package_default_value($('#year'), $('.default-year'), data.defaultYear);
    package_menu(data.month, $('#default-month-menu'));
    package_default_value($('#month'), $('.default-month'), data.defaultMonth);
    package_menu(data.days, $('#default-day-menu'));
    package_default_value($('#day'), $('.default-day'), data.defaultDay);
    package_menu(data.productCode, $('#default-productC-menu'));
    package_default_value($('#productC'), $('.default-productC'), data.defaultProductCode);
}
function package_default_value(target_input, targte_dafult, data) {
    target_input.val(data)
    targte_dafult.html(data);
}
function package_approve_info(var1,var2){
    let content="";
    $.each(var1,function (index,item){
        content+="<div class='sk'><div style='width: 100%;margin: 5% 0'><span style='margin-left: 39%;'>"+item.createTime+"</span></div>";
        content+="<div class='as'>" +
            "                    <img  src='/image/yz.png' class='ui medium rounded image'" +
            "                         style='width: 30px;height: 30px;float: left'" +
            "                         alt=''>" +
            "                    <div class='as-content'>" +
            "                        <div style='width: 100%;height: 70%;border-bottom: rgba(159,151,151,0.64) solid 1px;'>" +
            "                            <div class='head' style='height: 15%;margin: 3% 0 0 5%;'>" +
            "                                <span style='color: #ffac5e'><i class='chess bishop icon'></i>审批</span>" +
            "                            </div>" +
            "                            <div style='margin-top: 5%;'>" +
            "                                <span class='as-span'>工单号：<a data-tooltip='进入生产' data-position='right center' class='watchTemplate'>"+item.workOrderNumber+"</a></span>" +
            "                                <p style='margin-top: 3%;'></p>" +
            "                                <span class='as-span'>审核类型："+item.type+"</span>" +
            "                                <p style='margin-top: 3%;'></p>" +
            "                                <span class='as-span'>开始时间："+item.createTime+"</span>" +
            "                            </div>" +
            "                        </div>" +
            "                        <div style='width: 100%;height: 20%;margin-top: 7%;'>" +
            "                            <div style='float: left;text-align: center;width: 49%;color: red;'><span w-id='"+item.workOrderNumber+"' task-id='"+item.taskId+"' style='cursor: pointer;' class='reject'>拒绝</span></div> |" +
            "                            <div style='float: right;text-align: center;width: 49%;color: green;'><span w-id='"+item.workOrderNumber+"' task-id='"+item.taskId+"' style='cursor: pointer;' class='passed'>通过</span></div>" +
            "                        </div>" +
            "                    </div>" +
            "                </div></div>";
    });
    content+=pack_hi(var2);
    let tar=$('#ap-list');
    if (tar.children().length > 5) {
        content+="<p style='margin: 20px auto;'><a id='load-more'>加载更多....</a></p>";
    }
    tar.html(content);
}
let a_start=0;
let a_size=5;
$(document).on("one", "#load-more", function () {
    $.get({
        url: '/flow/loadMore/'+a_start+"/"+a_size,
        success:function (result){
            if (result.status === 200) {
                $('#ap-list').append(pack_hi(result.data));
                a_start=a_size;
                a_size+=a_size;
            }
        }
    })
})

function pack_hi(var2){
    let content="";
    $.each(var2,function (index,item){
        content+="<div style='width: 100%;margin: 5% 0'><span style='margin-left: 39%;'>"+item.beginTime+"</span></div>";
        content+="<div class='as'>" +
            "                    <img  src='/image/yz.png' class='ui medium rounded image'" +
            "                         style='width: 30px;height: 30px;float: left'" +
            "                         alt=''>" +
            "                    <div class='as-content'>" +
            "                        <div class='head' style='height: 15%;margin: 3% 0 0 5%;'>" +
            "                            <span style='color: #ffac5e'><i class='chess bishop icon'></i>审批</span>" +
            "                        </div>" +
            "                        <div style='position: absolute;z-index: 1999;margin-left: 80%;margin-top: -6%;width: 16%;height: 25%;'>" +
            "                            <img" +
            "                                    src='"+item.img+"' style='width: 100%;height: 100%;' alt=''></div>" +
            "                        <div class='as-content-cc' ap-id='"+item.proInstId+"'>" +
            "                            <span style='margin:4% 0 0 5%;color: #070700;font-size: 16px;font-weight: bold;'>"+item.name+"发起的结案审核</span>" +
            "                            <p></p>" +
            "                            <span class='as-span'>审核类型："+item.type+"</span>" +
            "                            <p></p>" +
            "                            <span class='as-span'>开始时间："+item.beginTime+"</span>" +
            "                            <p></p>" +
            "                            <span class='as-span'>工单号："+item.workOrderNumber+"</span>" +
            "                        </div>" +
            "                    </div>" +
            "                </div>";
    })
    return content;
}


//编辑工单
$(document).on("click", ".edit-ticket", function () {
    content = open_edit($(this));
})

//取消修改
$(document).on("click", ".cancel-ticket", function () {
    close_edit(content, $(this));
});
//修改工单
$(document).on("click", ".save-ticket", function () {
    if (!$(this).prevAll('.edit-ticket').hasClass('disabled')) {
        failure("非法操作");
        return;
    }
    let data = $(this).parents('.ds').siblings();
    let tr = $(this).parents('tr');
    $('.edit-button').removeClass('disabled');
    update_ticket(data, tr);
});
function update_ticket(data, tr) {
    let workOrderNumber = data[1].innerText;
    if (verify_work_order_number(workOrderNumber)) {
        failure("非法工单");
        return;
    }
    $.post({
        url: '/plan/modifyWorkOrder',
        contentType: "application/json",
        data: JSON.stringify({
            "workOrderNumber": workOrderNumber,
            "orderNumber": data[2].innerText,
            "productCode": data[3].innerText,
            "productNumber": data[4].innerText,
            "productionQuantity": data[5].innerText,
            "planBeginTime": data[6].innerText,
            "planEndTime": data[7].innerText,
            "softwareVersion": data[9].innerText,
            "remarks": data[10].innerText
        }),
        success: function (result) {
            switch (result.status) {
                case 200:
                    data.removeAttr('contenteditable');
                    //data.first().toggleClass('disabled');
                    success(result.msg);
                    break;
                default:
                    console.log("编辑失败")
                    tr.replaceWith(content);
                    failure(result.msg);
            }
        }
    })
}

//****工单查询
//查询所有工单
function queryAllWorkOrder(start, pageSize) {
    $.get({
        url: '/plan/queryAllWorkOrder',
        data: {
            "pageSize": pageSize,
            "pageNum": start
        },
        success: function (result) {
            if (result.status === 200) {
                renderTheTable(result.data);

                renderTfoot(pageSize, result.data, 'queryAllWorkOrder', "1", start);
            }
        }
    })
}

$('#search').click(function (){
    search_work_order(start,pageSize,$('#searchVal').val().trim());
})

//文本框查询
function search_work_order(start, pageSize, searchValue) {
    $.post({
        url: '/plan/searchWorkOrder',
        data: {
            "pageSize": pageSize,
            "pageNum": start,
            "searchValue": searchValue
        },
        success: function (result) {
            if (result.status === 200) {
                renderTheTable(result.data);
                renderTfoot(pageSize, result.data, 'search_work_order', searchValue, start);

            }
        }
    })
}

//历史工单查询
laydate.render({
    elem: '#historicalTime',
    done: function (value) {
        if (verify_date_format(value)) {
            search_historical_tickets(start, pageSize, value);
        }
    }
});

//历史记录查询 historicalTime
function search_historical_tickets(start, pageSize, historicalTime) {
    $.post({
        url: '/plan/searchHistoricalTickets',
        data: {
            "pageSize": pageSize,
            "pageNum": start,
            "historicalTime": historicalTime
        },
        success: function (result) {
            if (result.status === 200) {
                renderTheTable(result.data);

                renderTfoot(pageSize, result.data, 'search_historical_tickets', historicalTime, start);
            }
        }
    })
}

$('#work-order-status').change(function () {
    query_work_order_by_status(start, pageSize, $(this).val());
});

//通过状态查询工单
function query_work_order_by_status(start, pageSize, value) {
    $.get({
        url: '/plan/queryByStatus',
        data: {
            "pageNum": start,
            "pageSize": pageSize,
            "status": value
        },
        success: function (result) {
            if (result.status === 200) {
                renderTheTable(result.data);
                renderTfoot(pageSize, result.data, 'query_work_order_by_status', value, start);
            }
        }
    })
}
//工单信息验证
$('#work-form').form({
    inline: true,
    on: 'submit',
    keyboardShortcuts: false,
    fields: {
        workType: {
            identifier: 'workType',
            rules: [
                {
                    type: 'empty',
                    prompt: '请选择工单类型'
                }
            ]
        },
        orderNumber: {
            identifier: 'orderNumber',
            rules: [
                {
                    type: 'empty',
                    prompt: '请填写订单号'
                }
            ]
        },
        productCode: {
            identifier: 'productCode',
            rules: [
                {
                    type: 'empty',
                    prompt: '请填写产品编码'
                }
            ]
        },
        productNumber: {
            identifier: 'productNumber',
            rules: [
                {
                    type: 'empty',
                    prompt: '请填写产品型号'
                }
            ]
        },
        productionQuantity: {
            identifier: 'productionQuantity',
            rules: [
                {
                    type: 'empty',
                    prompt: '请填写生产数量'
                },
                {
                    type: 'regExp',
                    value: /^([0-9])+$/,
                    prompt: '数据格式错误'
                }
            ]
        },
        // planBeginTime: {
        //     identifier: 'planBeginTime',
        //     rules: [
        //         {
        //             type: 'empty',
        //             prompt: '请选择开始时间'
        //         }
        //     ]
        // },
        // planEndTime: {
        //     identifier: 'planEndTime',
        //     rules: [
        //         {
        //             type: 'empty',
        //             prompt: '请选择结束时间'
        //         }
        //     ]
        // }
        // ,
        softwareVersion: {
            identifier: 'softwareVersion',
            rules: [
                {
                    type: 'empty',
                    prompt: '请填写软件版本'
                },
                {
                    type: 'regExp',
                    value: /^[A-Za-z.\-_0-9]{3,50}$/,
                    prompt: "模板名称格式错误"
                }
            ]
        }
        ,
        remarks: {
            identifier: 'remarks',
            rules: [
                {
                    type: 'empty',
                    prompt: '请填写备注.例: 无'
                }
            ]
        }
    },
    onSuccess: function () {
        addWok();
    }
})

//添加新的工单
function addWok() {
    $.post({
        url: '/plan/addWorkOrder',
        contentType: "application/json",
        data: JSON.stringify({
            "workType": $('#workType').val(),
            "orderNumber": $('#orderNumber').val(),
            "productCode": $('#productCode').val(),
            "productNumber": $('#productNumber').val(),
            "productionQuantity": $('#productionQuantity').val(),
            "planBeginTime": $('#planBeginTime').val(),
            "planEndTime": $('#planEndTime').val(),
            "softwareVersion": $('#softwareVersion').val(),
            "remarks": $('#remarks').val()
        }),
        success: function (result) {
            if (result.status===200) {
                    $('#work-form').form('clear');
                    success(result.msg);
            }
        }
    })
}


//渲染表格数据
function renderTheTable(data) {
    let lists = data.list;
    let content = "";
    let tails;
    for (let i = 0; i < lists.length; i++) {
        tails = get_tails(lists[i].status);
        content += "<tr style='text-align: center'>" +
            "<td class='ds disabled'>" + parseInt(i + 1) + "</td>" +
            "<td class='ds' data-tooltip='进入生产' data-position='left center'><div class='ui button watchTemplate'>" + lists[i].workOrderNumber + "</div></td>" +
            "<td data-tooltip='" + lists[i].orderNumber + "' data-position='top center' class='copy'><div class='remarks' style='width: 85px!important;'>" + lists[i].orderNumber + "</div></td>" +
            "<td>" + lists[i].productCode + "</td>" +
            "<td>" + lists[i].productNumber + "</td>" +
            "<td>" + lists[i].productionQuantity + "</td>" +
            "<td>" + lists[i].planBeginTime + "</td>" +
            "<td>" + lists[i].planEndTime + "</td>" +
            "<td class='ds'>" + lists[i].ClosingTime + "</td>" +
            "<td data-tooltip='" + lists[i].softwareVersion + "' data-position='top center' class='copy'><div class='remarks' style='width: 100px!important;'>" + lists[i].softwareVersion + "</td>" +
            "<td data-tooltip='" + lists[i].remarks + "' data-position='top center' class='copy'><div class='remarks' style='width: 100px!important;'>" + lists[i].remarks + "</div></td>" +
            "<td class='ds'>" + lists[i].status + "</td>" +
            "<td class='ds'> <div class='ui dropdown item'>" +
            "                    Operation" +
            "                    <i class='dropdown icon'></i>" +
            "                    <div class='menu'>" +
            tails+
            "</div></div></td>" +
            "</tr>"
    }
    $('tbody').html(content);
    $('.ui.dropdown').dropdown({
        on: 'click'
    });
    $('#total').html("共" + data.pages + "页 " + data.total + " 条记录" + "&nbsp;&nbsp;当前页: " + data.pageNum);
}


    //刷新工单
    $('#reload-work-order').click(function () {
        if (flag) {
            $('#historicalTime').val("");
            $('#searchVal').val("");
            $('#work-order-status').val("");
            $('#status-text').html("请选择工单状态").addClass('default');
            queryAllWorkOrder(start, pageSize, null);
        }
    });
    //查看工单模板
    $(document).on("click", ".watchTemplate", function () {
        let workOrderNumber = $(this).text();
        $.get({
            url: '/index/check/checkTemplatePage',
            data: {
                "workOrderNumber": workOrderNumber
            },
            success: function (result) {
                if (result.status === 200) {
                    window.location.href = ("/index/page/template/" + workOrderNumber);
                }
            }
        })
    })

$(document).on('click', '.closing', function () {
    let cur = $(this);
    Notiflix.Confirm.Show('手动结案', '', '确定', '取消', function () {
        let workOrderNumber = cur.parent().siblings()[1].innerText;
        closing_ticket(workOrderNumber, cur);
    }, function () {
    });
})
function closing_ticket(workOrderNumber, target) {
    if (verify_work_order_number(workOrderNumber)) {
        failure("非法工单");
        return;
    }
    $.post({
        url: '/plan/workOrderSettlement',
        data: {
            "workOrderNumber": workOrderNumber
        },
        success: function (result) {
            if (result.status === 200) {
                target.parent().parent().remove();
                success(result.msg);
            }
        }
    })
}

$(document).on('click', '.anti-closing', function () {
    let cur = $(this);
    Notiflix.Confirm.Show('反结案', '', '确定', '取消', function () {
        let workOrderNumber = cur.parent().siblings()[1].innerText;
        anti_closing(workOrderNumber, cur);
    }, function () {
    });
})
function anti_closing(workOrderNumber, target) {
    if (verify_work_order_number(workOrderNumber)) {
        failure("非法工单");
        return;
    }
    $.get({
        url: '/plan/caseClosedReversal',
        data: {
            "workOrderNumber": workOrderNumber
        },
        success: function (result) {
            if (result.status === 200) {
                target.parent().parent().remove();
                success(result.msg);
            }
        }
    })
}

$(document).on('click', '.delete-ticket', function () {
    let cur = $(this);
    Notiflix.Confirm.Show('删除工单', '确当要删除吗？', '确定', '取消', function () {
        let workOrderNumber = cur.parent().siblings()[1].innerText;
        delete_work_order(workOrderNumber, cur);
    }, function () {
    });
})
function delete_work_order(workOrderNumber, target) {
    if (verify_work_order_number(workOrderNumber)) {
        failure("非法工单");
        return;
    }
    $.get({
        url: '/plan/deleteWorkOrder',
        data: {
            "workOrderNumber": workOrderNumber
        },
        success: function (result) {
            if (result.status === 200) {
                target.parent().parent().remove();
                success(result.msg);
            }
        }
    })
}
$(document).on('click', '.anti_completion', function () {
    let cur = $(this);
    Notiflix.Confirm.Show('反完工', '确定要反完工吗？', '确定', '取消', function () {
        let workOrderNumber = cur.parent().siblings()[1].innerText;
        anti_completion(workOrderNumber, cur);
    }, function () {
    });
})
function anti_completion(workOrderNumber, target) {
    if (verify_work_order_number(workOrderNumber)) {
        failure("非法工单");
        return;
    }
    $.get({
        url: '/plan/antiFinishedWorkOrder',
        data: {
            "workOrderNumber": workOrderNumber
        },
        success: function (result) {
            if (result.status === 200) {
                target.parent().parent().remove();
                success(result.msg);
            }
        }
    })
}

$(document).on('click', '.reject', function () {
    let cur = $(this);
    Notiflix.Confirm.Show('结案审批', '确定要驳回吗？', '确定', '取消', function () {
        reject(cur.attr('w-id'),cur.attr('task-id'),cur)
    }, function () {
    });
})

function reject(number,id,target){
    $.get({
        url: '/flow/reject/'+number+"/"+id,
        success:function (result){
            if (result.status === 200) {
                success("操作成功");
                target.parents('.sk').remove();
            }
        }
    })
}


$(document).on('click', '.passed', function () {
    let cur = $(this);
    Notiflix.Confirm.Show('结案审批', '确定要通过吗？', '确定', '取消', function () {
        passed(cur.attr('w-id'),cur.attr('task-id'),cur)
    }, function () {
    });
})

function passed(number,id,target){
    $.get({
        url: '/flow/approve/'+number+"/"+id,
        success:function (result){
            if (result.status === 200) {
                success("操作成功");
                target.parents('.sk').remove();
            }
        }
    })
}

function load_produce_information() {
    let year = $('#year').val();
    let month = $('#month').val();
    let day = $("#day").val();
    let productCode = $('#productC').val();
    let data = [];

    $.get({
        url: '/plan/statisticsOfFinishedMachines',
        async: false,
        data: {
            "year": year,
            "month": month,
            "day": day,
            "productCode": productCode
        },
        success: function (result) {
            if (result.status === 200) {
                if (result.data !== null) {
                    data = result.data;
                    beforeData = data;
                }
            } else {
                data = beforeData;
            }
        }
    });
    return data;
}
$('#year').change(function () {
    canvas(load_produce_information());
})
$('#month').change(function () {
    canvas(load_produce_information());
})

$('#day').change(function () {
    canvas(load_produce_information());
})
$('#productC').change(function () {
    canvas(load_produce_information());
});

$('#export-year').click(function () {
    let year = $('#year').val();
    if (null === year) {
        failure("日期错误")
        return;
    }
    let url = "/plan/annualReportExport?year=" + year;
    download("生产信息统计年报(" + year + ")-" + new Date().getTime() + '.xlsx', url);
})

$('#export-month').click(function () {
    let year = $('#year').val();
    let month = $('#month').val();
    if (year === null || null === month) {
        failure("日期错误")
        return;
    }
    let time = year + "-0" + month + "-";
    let url = "/plan/monthlyReportExport?year=" + year + "&month=" + month;
    download("生产信息统计月报(" + time + ")-" + new Date().getTime() + '.xlsx', url);
});

$('#export-day').click(function () {
    let year = $('#year').val();
    let month = $('#month').val();
    let day = $('#day').val();
    if (year === null || month === null || day === null) {
        failure("日期错误")
        return;
    }
    let time = year + "-0" + month + "-0" + day + "-";
    let url = "/plan/dayReportExport?year=" + year + "&month=" + month + "&day=" + day;
    download("生产信息统计日报(" + time + ")-" + new Date().getTime() + '.xlsx', url);
});


$('#bindEmail').click(function (){
    show_modal($('.bindEmailModal'));
})
$('#emailFrom').form({
    on: 'submit',
    inline: true,
    keyboardShortcuts:false,
    fields:{
        email:{
            identifier: "email",
            rules:[
                {
                    type: 'empty',
                    prompt: '请输入邮箱'
                },
                {
                    type: 'regExp',
                    value: /^(\w+)([.]\w+)@timevar.com$/,
                    prompt: "邮箱格式错误"
                }
            ]
        }
    },
    onSuccess:function (){
        bind_email();
    }
})
function bind_email(){
    let email=$('#email').val().trim();
    $.get({
        url: '/admin/bindingEmail/'+email,
        success:function (result){
            if (result.status === 200) {
                success(result.msg);
                $('#emailFrom').form('reset');
                $('.bindEmailModal').modal('hide');
            }
        }
    })
}


let update_password_form = $('#update-password-form');

$('.ui.rest-password.reset.button').click(function () {
    update_password_form.form('reset');
})

$('#update-password').click(function () {
    show_modal($('.ui.modal.update-password'));
    $('#updatePassword-btn').removeClass('disabled');
});
update_password_form.form({
    inline: true,
    on: "submit",
    keyboardShortcuts: false,
    fields: {
        "new-password": {
            identifier: "new-password",
            rules: [
                {
                    type: 'empty',
                    prompt: '请填写密码'
                },
                {
                    type: "regExp",
                    value: /^[0-9a-z]{6,12}$/,
                    prompt: "格式错误"
                }
            ]
        }
    },
    onSuccess: function () {
        let target = $('#updatePassword-btn');
        target.addClass('disabled');
        update_password(target);
    }
});

function update_password(target) {
    let password = $('#new-password').val();
    let sure_password = $('#sure-password').val();
    if (password !== sure_password) {
        update_password_form.form('add prompt', "sure-password", "两次密码不一致");
        target.removeClass('disabled');
        return;
    }
    $.post({
        url: "/index/updatePassword",
        data: {
            "password": password
        },
        success: function (result) {
            if (result.status === 200) {
                $('.ui.modal.update-password').modal('hide');
                success("修改成功");
            }
        }
    })
}
let vo;
let exp = document.getElementById('exp');
let first = true;
$('#relax').click(function () {
    show($('#video'));
    document.body.style.overflow = 'hidden';
    if (first) {
        $.get({
            url: '/plan/getVideoLink',
            success:function (result){
                if (result.status === 200) {
                    vo=result.data;
                    console.log(vo);
                    exp.src = result.data[0];
                    exp.play();
                }
            }
        })
        first = false;
    }
});
let idx = 0;
$(document).on("mousewheel DOMMouseScroll", '.v-play', function (event) {
    let delta = (event.originalEvent.wheelDelta && (event.originalEvent.wheelDelta > 50 ? 1 : -1)) ||  // chrome & ie
        (event.originalEvent.detail && (event.originalEvent.detail > 50 ? -1 : 1));              // firefox
    if (delta > 0 && idx > 0) {
        // 向上滚
        idx--;
        exp.src = vo[Math.max(0, idx)];
        exp.play();
    } else if (delta < 0 && idx < vo.length - 1) {
        // 向下滚
        idx++;
        exp.src = vo[Math.min(vo.length - 1, idx)];
        exp.play();
    }
});




//*****尾巴拼接
const edit_t = "<a class='item edit-ticket'><i class='edit outline icon'></i>编辑</a>";

const cancel_t="<a class='item cancel-ticket'><i class='reply  icon'></i>取消</a>";

const save_t="<a class='item save-ticket'><i class='save outline icon'></i>保存</a>";

const anti_closing_t="<a class='item anti-closing'><i class='reply  icon'></i>反结案</a>";

const closing_t="<a class='item closing'><i class='reply  icon'></i>结案</a>";

const anti_completion_t="<a class='item anti_completion'><i class='reply  icon'></i>反完工</a>";

const delete_t="<a class='item delete-ticket'><i class='reply  icon'></i>删除</a>";

const public_t=edit_t + cancel_t + save_t;


function get_tails(val) {
    let tails;
    switch (val) {
        case "完工":
            tails = closing_t + anti_completion_t;
            break;
        case "结案":
            tails = anti_closing_t;
            break;
        case "下达":
            tails = public_t + delete_t;
            break;
        case "异常":
            tails = public_t;
            break;
        case "在制":
            tails = public_t;
            break;
    }
    return tails;
}